package com.ch.base.util;


public class NumberUtil {
	/**
	 * <b>中文序號轉阿拉伯數字</b><br/>
	 * 如：一○○ -> 100, 一○一 -> 101 
	 * @param	origStr	欲轉換之字串
	 * @return	阿拉伯數字 (String 型態)
	 */
	public static String stringToNum(String origStr){
		String result = "";
		String[] numbers={"○", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};
		for (int i=0; i<origStr.length(); i++) {
			for (int j=0; j<numbers.length; j++) {
				String num = "";
				if (origStr.substring(i, i+1).equals(numbers[j])) {
					if (j == 10) {
						if (origStr.length() == 1) {
							num = "10";
						} else if (origStr.length() == 2) {
							if (i == 0) {
								num = "1";
							} else {
								num = "0";
							}
						}
					} else {
						num = String.valueOf(j);
					}
				}
				result = result + num;
			}
		}
		return result;
	}
	
	/**
	 * <b>阿拉伯數字轉中文數字</b><br/>
	 * 如：100 - >一百, 103 -> 一百零三, 133, 一百三十三<br/>
	 * 注意：本方法僅提供 3 位數以下之整數數字轉換<br/>
	 * @param 	origNum 欲轉換之整數值
	 * @return 	中文數字
	 */
	public static String numToChineseNum(int origNum){
		String result = "";
		if (origNum <=10) {
			switch (origNum){
			case 1:		result = "一";	break;
			case 2:		result = "二";	break;
			case 3:		result = "三";	break;
			case 4:		result = "四";	break;
			case 5:		result = "五";	break;
			case 6:		result = "六";	break;
			case 7:		result = "七";	break;
			case 8:		result = "八";	break;
			case 9:		result = "九";	break;
			case 10:	result = "十";	break;
			}
		}else if (origNum < 100) {
			if ((origNum % 10) == 0) {
				if (origNum == 10) {
					result = "十";
				} else {
					result = numToChineseNum(Integer.parseInt(("" + origNum).substring(0,1))) + "十";
				}
			} else {
				if (origNum >= 10  && origNum < 20) {
					result = "十" + numToChineseNum(origNum % 10);
				}else{
					result = numToChineseNum(Integer.parseInt(("" + origNum).substring(0,1))) + "十" + numToChineseNum(origNum % 10);
				}
			}
		} else if (origNum < 1000) {
			if ((origNum % 100) < 10) {
				if ((origNum % 100) == 0) {
					result = numToChineseNum(Integer.parseInt(("" + origNum).substring(0,1))) + "百";
				}else{
					result = numToChineseNum(Integer.parseInt(("" + origNum).substring(0,1))) + "百零" + (numToChineseNum(origNum % 100));
				}
			} else {
				if (((origNum % 100) >= 10)  && ((origNum % 100) < 20)) {
					result = numToChineseNum(Integer.parseInt(("" + origNum).substring(0,1))) + "百一" + (numToChineseNum(origNum % 100));
				}else{
					result = numToChineseNum(Integer.parseInt(("" + origNum).substring(0,1))) + "百" + (numToChineseNum(origNum % 100));
				}
			}
		}else {
			result = null;
		}
		return result;
	}
	
}
